﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace MessageProtocol
{
    public class MessageConstants
    {
        public const uint TYPE_REQUEST_PING = 0x00;
        public const uint TYPE_REQUEST_ECHO = 0x01;
        public const uint TYPE_REQUEST_SAVE = 0x02;
        public const uint TYPE_REQUEST_GET = 0x03;

        public const byte NOT_LASTMSG = 0x00;
        public const byte LASTMSG = 0x01;
    }

    public interface ISerializable
    {
        byte[] GetBytes();
        int GetSize();
        object GetObject(byte[] bytes);
    }

    public class Message 
    {
        public Header Header{ get; set;}
        public ISerializable Body { get; set; }

        public byte[] GetBytes()
        {
            byte[] bytes = new byte[GetSize()];

            Header.GetBytes().CopyTo(bytes, 0);
            Body.GetBytes().CopyTo(bytes, Header.GetSize());

            return bytes;
        }
        public int GetSize()
        {
            return Header.GetSize() + Body.GetSize();
        }


        public object GetObject(byte[] bytes)   

        {
            Message msg = new Message();

            Header header = new Header();
            header.MSGID = BitConverter.ToUInt32(bytes, 0);
            header.MSGTYPE = BitConverter.ToUInt32(bytes, 4);
            header.BODYLENGTH = BitConverter.ToUInt32(bytes, 8);
            header.LASTMSG = bytes[12];
 

            throw new NotImplementedException();
        }
    }

}
